#include "note.h"
#include <QPainter>
#include "pdlg_note.h"

NoteShape *NoteShape::clone() const
{
	NoteShape *newshape = new NoteShape();
	newshape->setPos(this->pos());
	newshape->setSize(this->size());
	newshape->setTitle(this->title());
	newshape->setDescription(this->description());
	return newshape;
}

NoteShape::NoteShape (QGraphicsItem *parent)
	: ResizableShape(parent)
{
	setTitle(tr("Note"));
}


NoteShape::NoteShape (const QPointF &pos, QGraphicsItem *parent)
	: ResizableShape(pos, parent)
{
	setTitle(tr("Note"));
}

/**
 * Задает текст комментария.
 * @param newtext Новый текст.
 */
void NoteShape::setTitle (const QString &newtext)
{
	prepareGeometryChange();
	_title = newtext;
}

/**
 * @return Текст комментария
 */
const QString &NoteShape::title() const
{
	return _title;
}

/**
 * Задает текст подсказки.
 * @param newtext Новый текст.
 */
void NoteShape::setDescription (const QString &newtext)
{
	setToolTip(newtext);
	_description = newtext;
}

/**
 * @return Текст подсказки
 */
const QString &NoteShape::description() const
{
	return _description;
}

void NoteShape::paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	painter->setBrush(Shape::innerBrush());
	painter->setPen(Qt::black);
	painter->drawPath(shape());
	QRectF r = boundingRect();
	painter->drawPolygon(QPolygonF()
		 << QPointF(r.width()-7, 0)
		 << QPointF(r.width()-7, 7)
		 << QPointF(r.width(), 7)
	);
	painter->setFont(Shape::basicFont());
	QTextOption textoption;
	textoption.setAlignment(Qt::AlignLeft | Qt::AlignTop);
	textoption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
	painter->drawText(
			r.adjusted(5,5,-5,-5),
			elidedText(_title, painter->font(), size()-QSizeF(11,11), textoption),
			textoption);
	if (isSelected())
	{
		setPenAndBrushForSelection(painter);
		painter->drawRect(r.adjusted(3,3,-3,-3));
	}
}


QPainterPath NoteShape::shape() const
{
	QRectF r = boundingRect();
	QPainterPath path;
	path.addPolygon(QPolygonF()
		<< QPointF(0,0)
		<< QPointF(r.width()-7, 0)
		<< QPointF(r.width(), 7)
		<< QPointF(r.width(), r.height())
		<< QPointF(0, r.height())
		<< QPointF(0,0)
	);
	return path;
}

void NoteShape::editProperties()
{
	NoteShapePropertiesDialog(this).exec();
}
